package cn.takovh.zpl.bean;

import org.dom4j.Element;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class Head implements ElementBean {
    private Guid guid;
    private List<Meta> metaList;
    private Title title;

    public Head() {
    }

    public Head(List<Meta> metaList, Title title) {
        this.metaList = metaList;
        this.title = title;
    }

    public Head(String title, int itemCount, int totalDuration) {
        this.metaList = new ArrayList<>();
        metaList.add(new Meta("itemCount", String.valueOf(itemCount)));
        metaList.add(new Meta("totalDuration", String.valueOf(totalDuration)));
        metaList.add(new Meta("generator", "Entertainment Platform -- 10.20112.1011.0"));
        this.title = new Title(title);
    }

    public Guid getGuid() {
        return guid;
    }

    public void setGuid(Guid guid) {
        this.guid = guid;
    }

    public List<Meta> getMetaList() {
        return metaList;
    }

    public void setMetaList(List<Meta> metaList) {
        this.metaList = metaList;
    }

    public Title getTitle() {
        return title;
    }

    public void setTitle(Title title) {
        this.title = title;
    }

    @Override
    public void getElement(Element root) {
        for (Meta meta : metaList) {
            Element child = root.addElement("meta");
            meta.getElement(child);
        }
        Element child2 = root.addElement("title");
        title.getElement(child2);
        Element child3 = root.addElement("guid");
        title.getElement(child3);
    }

    @Override
    public void parseElement(Element root) {
        this.metaList = new ArrayList<>();
        this.title = new Title();
        this.guid = new Guid();

        for (Iterator<Element> it = root.elementIterator(); it.hasNext(); ) {
            Element element = it.next();
            String name = element.getName();
            switch (name) {
                case "meta":
                    Meta meta = new Meta();
                    meta.parseElement(element);
                    this.metaList.add(meta);
                    break;
                case "title":
                    this.title.parseElement(element);
                    break;
                case "guid":
                    this.guid.parseElement(element);
                    break;
                default:
                    throw new IllegalArgumentException(name);
            }
        }
    }
}
